package com.wizinno.android.urineflow.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.wizinno.android.urineflow.R;
import com.wizinno.android.urineflow.activity.MainActivity;
import com.wizinno.android.urineflow.constant.CommonControl;
import com.wizinno.android.urineflow.constant.FragmentNoConstant;
import com.wizinno.android.urineflow.mqtt.MqttPubTopic;
import com.wizinno.android.urineflow.mqtt.vo.InspectData;
import com.wizinno.android.urineflow.mqtt.vo.InspectDataVO;
import com.wizinno.android.urineflow.mqtt.vo.PatientInfo;
import com.wizinno.android.urineflow.serialport.SerialPortUtil;
import com.wizinno.android.urineflow.service.MQTTService;
import com.wizinno.android.urineflow.util.FileUtil;

import java.util.List;

public class ReportLeftFragment extends BaseFragment {

    private TextView tv_max_vol;
    private TextView tv_max_rate;
    private TextView tv_avg_rate;

    public ReportLeftFragment() {
        setNo(FragmentNoConstant.REPORT_LEFT);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initData() {
        List<Float> weightList = MainActivity.weightList;
        List<Float> rateList = MainActivity.rateList;
        if (CollUtil.isNotEmpty(weightList)) {
            float maxVol = CollectionUtil.max(weightList);
            float maxRate = CollectionUtil.max(rateList);
            float sumRate = 0;
            for (Float aFloat : rateList) {
                sumRate += aFloat;
            }
            float avgRate = sumRate / rateList.size();
            tv_max_vol.setText((int) maxVol + " ml");
            tv_max_rate.setText((int) maxRate + " ml/s");
            tv_avg_rate.setText((int) avgRate + " ml/s");
            SerialPortUtil.wash();
            sendData(weightList, maxVol, maxRate, avgRate);
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tv_max_vol = view.findViewById(R.id.tv_max_vol);
        tv_max_rate = view.findViewById(R.id.tv_max_rate);
        tv_avg_rate = view.findViewById(R.id.tv_avg_rate);
    }

    private void sendData(List<Float> weightList, float maxVol, float maxRate, float avgRate) {
        //发送数据
        InspectDataVO data = new InspectDataVO();
        String noStr = FileUtil.readInspectNo();
        Long no = 1L;
        if (StrUtil.isNotBlank(noStr)) {
            no = Long.parseLong(noStr) + 1;
        }
        data.setNo(no);
        data.setSn(CommonControl.INSTANCE.DEVICE_SN);
        InspectData inspectData = new InspectData();
        inspectData.setAverageFlowRate(avgRate);
        inspectData.setDataEffective(true);
        inspectData.setVoidingTime((float) (weightList.size() * 0.1 / 5));
        int count = CollectionUtil.count(weightList, aFloat -> aFloat > 0);
        inspectData.setFlowTime((float) (count * 0.1 / 5));
        inspectData.setInspectTime(System.currentTimeMillis());
        inspectData.setMaxFlowRate(maxRate);
        inspectData.setSampleFrequency(5);
        inspectData.setInspectData(weightList);
        float timeToMaxFlow = 0;
        for (Float aFloat : weightList) {
            timeToMaxFlow += 0.2;
            if (aFloat == maxVol) {
                break;
            }
        }
        inspectData.setTimeToMaxFlow(timeToMaxFlow);
        inspectData.setWaitingTime(0f);
        PatientInfo patientInfo = new PatientInfo();
        patientInfo.setPatientName("匿名");
        patientInfo.setSex("男");
        patientInfo.setDepartment("匿名");
        data.setPatientInfo(patientInfo);
        data.setData(inspectData);
        //修改NO存储值
        FileUtil.writeInspectNo(String.valueOf(no));
        boolean publish = MQTTService.publish(MqttPubTopic.DATA_INSPECTION, JSON.toJSONString(data));
        if (!publish) {
            //发布失败，存储到文件
            FileUtil.writeInspectData(data);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_report_left, container, false);
    }
}